package com.yanqu.road.dao.impl.activity.summerquiz;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.summerquiz.config.SummerQuizInfo;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SummerQuizConfigDaoImpl extends TempDao {

    private static String getSummerQuizInfoSql = "select * from `t_s_activity_summer_quiz` where `activityId`=? ORDER BY `id` ASC";
    public List<SummerQuizInfo> getSummerQuizInfoList(int activityId) {
        List<SummerQuizInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSummerQuizInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SummerQuizInfo data = new SummerQuizInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setDate(DateHelper.str2timestamp(rs.getString("date"), "yyyy-MM-dd"));
                data.setBeginQuiz(DateHelper.str2timestamp(rs.getString("beginQuiz"), "yyyy-MM-dd"));
                data.setBeginQuizEx(Integer.parseInt(rs.getString("beginQuizEx")) * DateHelper.HOUR_MILLIONS);
                data.setEndQuiz(DateHelper.str2timestamp(rs.getString("endQuiz"), "yyyy-MM-dd"));
                data.setEndQuizEx(Integer.parseInt(rs.getString("endQuizEx")) * DateHelper.HOUR_MILLIONS);
                data.setInitialOdds(StringUtils.stringToIntegerList(rs.getString("initialOdds"), "\\|"));
                data.setQuestion(StringUtils.stringToStringList(rs.getString("question"), "\\|"));
                data.setProject(StringUtils.stringToStringList(rs.getString("project"), "\\|"));
                data.setBannerType(rs.getInt("bannerType"));
                data.setResult(rs.getInt("result"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
